package com.mnt.washio.activities;

import java.util.Random;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mnt.washio.R;
import com.mnt.washio.objects.Coupon;
import com.mnt.washio.utils.SessionManager;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.mnt.washio.utils.network.ws.BaseWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl.WebServiceFlag;
import com.mnt.washio.utils.network.ws.CreateCouponWSControl;
import com.mnt.washio.utils.network.ws.GetCouponWSControl;
import com.nikmesoft.nmsharekit.NMShareKit;
import com.nikmesoft.nmsharekit.configers.NMSKDevDefine;
import com.nikmesoft.nmsharekit.objects.NMShareMessage;
import com.nikmesoft.nmsharekit.utils.NMSKFacebookSession;
import com.parse.ParseUser;

@SuppressWarnings("deprecation")
public class ShareActivity extends BaseActivity implements OnClickListener,
		WebServiceCommunicatorListener {

	private ImageView mImvShareFB, mImvShareTT, mImvShareMail, mImvShareSMS;
	private View mBtnCopy;
	private TextView mTvMyCode;
	private Coupon mCoupon;
	private ProgressDialog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);
		initView();
		initListeners();
		mCoupon = SessionManager.getSessionManager(this).getCoupon();
		if (mCoupon != null) {
			mTvMyCode.setText("Your code: " + mCoupon.getCode());
		} else {
			new GetCouponWSControl(this, this, ParseUser.getCurrentUser()
					.getObjectId()).execute();
		}
	}

	public static String generateCode() {
		Random rd = new Random();
		// DecimalFormat formater = new DecimalFormat("00000");
		return String.valueOf(rd.nextInt(100000));
	}

	@Override
	protected void initView() {
		super.initView();

		mLoadingDialog = new ProgressDialog(this);
		mLoadingDialog.setMessage("Loading...");
		mLoadingDialog.setCancelable(false);

		// myCode = ParseUser.getCurrentUser().getString("first_name")
		// + generateCode();

		mImvShareFB = (ImageView) findViewById(R.id.imv_share_fb);
		mImvShareTT = (ImageView) findViewById(R.id.imv_share_tt);
		mImvShareMail = (ImageView) findViewById(R.id.imv_share_mail);
		mImvShareSMS = (ImageView) findViewById(R.id.imv_share_sms);
		mBtnCopy = findViewById(R.id.btn_copy);
		mTvMyCode = (TextView) findViewById(R.id.tv_my_code);
		// mTvMyCode.setText("Your Code: " + myCode);
	}

	private void initListeners() {
		mImvShareFB.setOnClickListener(this);
		mImvShareTT.setOnClickListener(this);
		mImvShareMail.setOnClickListener(this);
		mImvShareSMS.setOnClickListener(this);
		mBtnCopy.setOnClickListener(this);
	}

	private String getMessageToShare() {
		return getString(R.string.text_to_share, mCoupon.getCode(), "$10");

	}

	@Override
	public void onClick(View v) {
		if (mCoupon != null) {
			NMShareMessage message;
			switch (v.getId()) {
			case R.id.imv_share_fb:
				message = new NMShareMessage();
				message.setMessage(getMessageToShare());
				NMShareKit.sharedInstance(this, this).shareFB(message);
				break;
			case R.id.imv_share_tt:
				message = new NMShareMessage();
				message.setMessage(getMessageToShare());

				NMSKDevDefine.sharedInstance(this, this).setTwitterCallBack(
						"http://twitterapp.com");
				NMSKDevDefine.sharedInstance(this, this).setTwitterConsumerKey(
						getString(R.string.tw_app_key));
				NMSKDevDefine.sharedInstance(this, this).setTwitterSecretKey(
						getString(R.string.tw_app_secret));
				NMShareKit.sharedInstance(this, this).shareTwitter(message);
				break;
			case R.id.imv_share_mail:
				sendMail();
				break;
			case R.id.imv_share_sms:
				sendSMS();
				break;
			case R.id.btn_copy:
				ClipboardManager myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
				myClipboard.setText(mCoupon.getCode());
				Statics.showToast(this, "Coupon copied to clipboard");
				break;
			/*
			 * case R.id.btn_copy: ClipboardManager myClipboard; myClipboard =
			 * (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
			 * myClipboard.setText(myCode); Toast.makeText(this,
			 * "Copied to clipboard", Toast.LENGTH_SHORT) =======
			 * NMSKDevDefine.sharedInstance(this, this).setTwitterCallBack(
			 * "http://twitterapp.com"); NMSKDevDefine.sharedInstance(this,
			 * this).setTwitterConsumerKey( getString(R.string.tw_app_key));
			 * NMSKDevDefine.sharedInstance(this, this).setTwitterSecretKey(
			 * getString(R.string.tw_app_secret));
			 * NMShareKit.sharedInstance(this, this).shareTwitter(message);
			 * break; case R.id.imv_share_mail: sendMail(); break; case
			 * R.id.imv_share_sms: sendSMS(); break; default: break; } } else {
			 * Toast.makeText(this, "You don't have any code!",
			 * Toast.LENGTH_SHORT) .show();
			 */
			}
		}
	}

	private void sendMail() {
		try {
			Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
					Uri.fromParts("mailto", "support@getwashio.com", null));
			emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Send us Feedback");
			emailIntent.putExtra(Intent.EXTRA_TEXT,
					"Go on, tell us what you think.");

			startActivity(emailIntent);
		} catch (Exception e) {
		}

	}

	private void sendSMS() {
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
				+ Statics.WASHIO_PHONE));
		intent.putExtra("sms_body", getMessageToShare());
		startActivity(intent);
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		NMSKFacebookSession.getInstance(this).onActivityResult(this,
				requestCode, resultCode, data);
		if (requestCode == 1000 && resultCode == RESULT_OK) {
			setResult(RESULT_OK);
			finish();
			return;
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		mLoadingDialog.show();
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		mLoadingDialog.dismiss();
		Toast.makeText(this, error, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		mLoadingDialog.dismiss();
		if (flag == WebServiceFlag.GET_COUPON) {
			if (result != null) {
				mCoupon = (Coupon) result;
				mTvMyCode.setText("My Code: " + mCoupon.getCode());
				SessionManager.getSessionManager(this).setCoupon(mCoupon);
			} else {
				new CreateCouponWSControl(this, this, ParseUser
						.getCurrentUser().getObjectId(), ParseUser
						.getCurrentUser().getString("first_name")
						+ generateCode()).execute();
			}
		} else if (flag == WebServiceFlag.CREATE_COUPON) {
			if (result != null) {
				mCoupon = (Coupon) result;
				mTvMyCode.setText("My Code: " + mCoupon.getCode());
				SessionManager.getSessionManager(this).setCoupon(mCoupon);
			}
		}
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
		// TODO Auto-generated method stub

	}

}
